<template>
	<view>
		<!-- <image-cropper  ref="cropper" id="image-cropper" :limit_move="true" :disable_rotate="true" :width="width"
			:height="height" :imgSrc="src" @load="cropperload" @imageload="loadimage" @tapcut="clickcut">
		</image-cropper> -->
		<view class="image-option-box">
			<view @click="resetImage">
				<!-- <van-icon name="replay" size="50rpx" /> -->
			</view>
			<view @click="getImage">
				<!-- <van-icon name="success" size="50rpx" /> -->
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				src: '',
				imgObj: {},
				width: 250, //宽度
				height: 250, //高度
			};
		},
		onLoad(options) {
			//获取到image-cropper实例
			// this.cropper = this.$refs.cropper;
			// console.log(this.cropper.upload)
			// //开始裁剪
			// // this.setData({
			// //     src:"https://raw.githubusercontent.com/1977474741/image-cropper/dev/image/code.jpg",
			// // });
			// this.cropper.upload()
			// uni.showLoading({
			// 	title: '加载中'
			// })
		},
		onShow(){
			// this.cropper = this.$refs.cropper;
			// console.log(this.cropper)
			// //开始裁剪
			// // this.setData({
			// //     src:"https://raw.githubusercontent.com/1977474741/image-cropper/dev/image/code.jpg",
			// // });
			// this.cropper.upload()
			// uni.showLoading({
			// 	title: '加载中'
			// })
		},
		methods: {
			cropperload(e) {
				console.log("cropper初始化完成");
			},
			loadimage(e) {
			 console.log("图片加载完成", e.detail);
				uni.hideLoading();
				//重置图片角度、缩放、位置
				this.cropper.imgReset();
			},
			clickcut(e) {
				console.log(e.detail);
				//点击裁剪框阅览图片
			 uni.previewImage({
					current: e.detail.url, // 当前显示图片的http链接
					urls: [e.detail.url] // 需要预览的图片http链接列表
				})
			},
			resetImage(e) {
				this.cropper.imgReset()
			},
			getImage(e) {
				this.cropper.getImg((res) => {
					console.log(res)
					this.src = res.url
					this.imgObj = res
					this.sendAvatar()
				})
			},
			sendAvatar() {
				let _self = this
				const baseUrl = getApp().globalData.baseUrl
				//console.log(this.data.imgObj)
				uni.showLoading({
					title: '上传中'
				})
				uni.uploadFile({
					url: baseUrl + '/api/uploadAvatar',
					filePath: _self.src,
					name: 'source',
					header: {
						'token': uni.getStorageSync('token'),
						'Cookie': uni.getStorageSync('cookie'),
					},
					success(res) {
						uni.hideLoading()
						let result = JSON.parse(res.data)
						console.log(result)
						if (result.statusCode !== 0) {
							return uni.showToast({
								title: result.msg,
								icon: 'error'
							})
						}
						uni.navigateBack({
							complete: (res) => {},
						})
					},
					fail(error) {
						console.log(error)
						uni.hideLoading()
						uni.showToast({
							title: '上传失败',
							icon: 'error'
						})
					}
				})
			}
		}
	}
</script>

<style lang="less">
	.image-option-box {
		position: absolute;
		left: 0;
		bottom: 0;
		width: 100vw;
		box-sizing: border-box;
		padding: 40rpx 120rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		color: #fff;
		z-index: 200;
	}
</style>
